##柱形图
import json
from utils.getDataFromMysql import *
import numpy as np
from array import array
from pictureData.AttributeObject import *



from utils.expand import *
@timecost
def generateColumnChart(result_list:list) -> str:
    ## 提取所需年份
    year_list = []
    ##提取标题头
    title_list = []

    ##提取对象属性列表集合
    attribute_list = []

    json_result = {}

    if (len(result_list) != 0):
        for item in result_list:
            tmp = item.__dict__
            year_tmp = tmp.get('year')
            if (year_tmp is not None and (year_tmp not in year_list)):
                year_list.append(year_tmp)
            country_tmp = tmp.get('country')
            norm_tmp = tmp.get('norm')
            scale_tmp = tmp.get('units')
            title_tmp = str(country_tmp) + '_' + str(norm_tmp) + '_' + str(scale_tmp)
            title_list.append(title_tmp)
            data_tmp = tmp.get('data_value')

            attribute_tmp = AttributeNode(title_tmp, year_tmp, data_tmp)
            attribute_list.append(attribute_tmp)

    original_array = np.array(year_list)
    unique_year_list = np.unique(original_array).tolist()
    json_result["echartX"] = unique_year_list

    original_title_array = np.array(title_list)
    unique_title_list = np.unique(original_title_array).tolist()
    json_result["echartNav"] = unique_title_list

    ##定义表头属性
    columns = []
    year_dict = {'prop': 'year', 'label': '年份'}
    columns.append(year_dict)
    unique_title = {obj.getTitleName() for obj in attribute_list}
    if (len(unique_title) != 0):
        for title_name_tmp in unique_title:
            tmp_dict = {'prop': title_name_tmp, 'label': title_name_tmp}
            columns.append(tmp_dict)
    json_result["columns"] = columns

    ##构建表格对应的属性值
    tableData = []
    if (len(year_list) != 0):
        for year in year_list:
            tableData_sigle_dict = {}
            tableData_sigle_dict['year'] = year
            for column in columns:
                if (column['prop'] != "year"):
                    # 使用生成器表达式和内置的 next 函数查找符合条件的对象
                    result = next((obj for obj in attribute_list if
                                   obj.getYear() == year and obj.getTitleName() == column['prop']), None)
                    if (result is not None):
                        tableData_sigle_dict[column['prop']] = result.getData()
                    else:
                        tableData_sigle_dict[column['prop']] = ''
            tableData.append(tableData_sigle_dict)
    json_result['tableData'] = tableData

    ##构建echart折线图表数据
    echartDataLine = []
    if (len(unique_title_list) != 0):
        for title in unique_title_list:
            title_dict_tmp = {}
            title_dict_tmp['name'] = title
            title_dict_tmp['type'] = 'bar'
            data = [obj.getData() for obj in attribute_list if obj.getTitleName() == title]
            title_dict_tmp['data'] = data
            echartDataLine.append(title_dict_tmp)
    json_result['echartData'] = echartDataLine

    return json.dumps(json_result)

if __name__ == '__main__':
    sql_query = "select * from weo_data limit 50 "
    result_list = getDataBySql(sql_query)
    response = generateColumnChart(result_list)
    print(response)